package com.pen.interfece.config;

import com.pen.interfece.interceptor.AdminLoginInterceptor;
import com.pen.interfece.interceptor.UserLoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;
    @Autowired
    private UserLoginInterceptor userLoginInterceptor;

    @Value("${file.upload-dir}") // 从配置文件注入路径
    private String uploadDir;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(adminLoginInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login");

        registry.addInterceptor(userLoginInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login", "/user/register");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 手动添加静态资源映射（关键！）
        String imagePath = "file:" + uploadDir + "/images/";
        registry.addResourceHandler("/images/**")
                .addResourceLocations(imagePath);
        log.info("静态资源映射已生效: {} -> {}", "/images/**", imagePath);
    }
}